home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / bash-1.12 / dist / mailcheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-14  |  10.5 KB  |  419 lines

  1. /* mailcheck.c -- The check is in the mail... */
  2.  
  3. /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7. Bash is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with Bash; see the file COPYING.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include "posixstat.h"
  24. #include <sys/param.h>
  25. #include "shell.h"
  26.  
  27. #include "maxpath.h"
  28.  
  29. #ifndef NOW
  30. #define NOW ((time_t)time ((time_t *)0))
  31. #endif
  32.  
  33. typedef struct {
  34.   char *name;
  35.   time_t access_time;
  36.   time_t mod_time;
  37.   long file_size;
  38. } FILEINFO;
  39.  
  40. /* The list of remembered mail files. */
  41. FILEINFO **mailfiles = (FILEINFO **)NULL;
  42.  
  43. /* Number of mail files that we have. */
  44. int mailfiles_count = 0;
  45.  
  46. /* The last known time that mail was checked. */
  47. int last_time_mail_checked = 0;
  48.  
  49. /* Returns non-zero if it is time to check mail. */
  50. time_to_check_mail ()
  51. {
  52.   char *temp = get_string_value ("MAILCHECK");
  53.   time_t now = NOW;
  54.   unsigned seconds = 0;
  55.  
  56.   if ((!temp || sscanf (temp, "%u", &seconds) == 0) ||
  57.       ((now - last_time_mail_checked < seconds)))
  58.     return (0);
  59.  
  60.   return (1);
  61. }
  62.  
  63. /* Okay, we have checked the mail.  Perhaps I should make this function
  64.    go away. */
  65. reset_mail_timer ()
  66. {
  67.   last_time_mail_checked = NOW;
  68. }
  69.  
  70. /* Locate a file in the list.  Return index of
  71.    entry, or -1 if not found. */
  72. find_mail_file (file)
  73.      char *file;
  74. {
  75.   register int index = 0;
  76.  
  77.   while (index < mailfiles_count)
  78.     if (strcmp ((mailfiles[index])->name, file) == 0) return index;
  79.     else index++;
  80.   return (-1);
  81. }
  82.  
  83. /* Add this file to the list of remembered files. */
  84. add_mail_file (file)
  85.      char *file;
  86. {
  87.   struct stat finfo;
  88.   char *full_pathname ();
  89.   char *filename = full_pathname (file);
  90.   int index = find_mail_file (file);
  91.  
  92.   if (index > -1)
  93.     {
  94.       if (stat (filename, &finfo) == 0)
  95.     {
  96.       mailfiles[index]->mod_time = finfo.st_mtime;
  97.       mailfiles[index]->access_time = finfo.st_atime;
  98.       mailfiles[index]->file_size = (long)finfo.st_size;
  99.     }
  100.       free (filename);
  101.       return;
  102.     }
  103.  
  104.   mailfiles = (FILEINFO **)
  105.     xrealloc (mailfiles,
  106.           ((++mailfiles_count) * sizeof (FILEINFO *)));
  107.  
  108.   mailfiles[mailfiles_count - 1] = (FILEINFO *)xmalloc (sizeof (FILEINFO));
  109.   mailfiles[mailfiles_count - 1]->name = filename;
  110.   if (stat (filename, &finfo) == 0)
  111.     {
  112.       mailfiles[mailfiles_count - 1]->access_time = finfo.st_atime;
  113.       mailfiles[mailfiles_count - 1]->mod_time = finfo.st_mtime;
  114.       mailfiles[mailfiles_count - 1]->file_size = finfo.st_size;
  115.     }
  116.   else
  117.     {
  118.       mailfiles[mailfiles_count - 1]->access_time
  119.     = mailfiles[mailfiles_count - 1]->mod_time = (time_t)-1;
  120.       mailfiles[mailfiles_count - 1]->file_size = (long)-1;
  121.     }
  122. }
  123.  
  124. /* Reset the existing mail files access and modification times to zero. */
  125. reset_mail_files ()
  126. {
  127.   register int i;
  128.  
  129.   for (i = 0; i < mailfiles_count; i++)
  130.     {
  131.       mailfiles[i]->access_time = mailfiles[i]->mod_time = 0;
  132.       mailfiles[i]->file_size = (long)0;
  133.     }
  134. }
  135.  
  136. /* Free the information that we have about the remembered mail files. */
  137. free_mail_files ()
  138. {
  139.   while (mailfiles_count--)
  140.     {
  141.       free (mailfiles[mailfiles_count]->name);
  142.       free (mailfiles[mailfiles_count]);
  143.     }
  144.  
  145.   if (mailfiles)
  146.     free (mailfiles);
  147.  
  148.   mailfiles_count = 0;
  149.   mailfiles = (FILEINFO **)NULL;
  150. }
  151.  
  152. /* Return the full pathname of FILE.  Easy.  Filenames that begin
  153.    with a '/' are returned as themselves.  Other filenames have
  154.    the current working directory prepended.  A new string is
  155.    returned in either case. */
  156. char *
  157. full_pathname (file)
  158.      char *file;
  159. {
  160.   char *tilde_expand (), *disposer;
  161.  
  162.   if (*file == '~')
  163.     file = tilde_expand (file);
  164.   else
  165.     file = savestring (file);
  166.  
  167.   if (absolute_pathname (file))
  168.     if (*file == '/')
  169.       return (file);
  170.  
  171.   disposer = file;
  172.  
  173.   {
  174.     char *current_dir = (char *)xmalloc (2 + MAXPATHLEN + strlen (file));
  175.     if (!getwd (current_dir))
  176.       {
  177.     report_error (current_dir);
  178.     free (current_dir);
  179.     return ((char *)NULL);
  180.       }
  181.     strcat (current_dir, "/");
  182.  
  183.     /* Turn /foo/./bar into /foo/bar. */
  184.     if (strncmp (file, "./", 2) == 0)
  185.       file += 2;
  186.  
  187.     strcat (current_dir, file);
  188.     free (disposer);
  189.     return (current_dir);
  190.   }
  191. }
  192.  
  193. /* Return non-zero if FILE's mod date has changed. */
  194. file_mod_date_changed (file)
  195.      char *file;
  196. {
  197.   time_t time = (time_t)NULL;
  198.   struct stat finfo;
  199.  
  200.   int index = find_mail_file (file);
  201.   if (index != -1)
  202.     time = mailfiles[index]->mod_time;
  203.  
  204.   if (stat (file, &finfo) == 0)
  205.     {
  206.       if (finfo.st_size != 0)
  207.     return (time != finfo.st_mtime);
  208.     }
  209.   return (0);
  210. }
  211.  
  212. /* Return non-zero if FILE's access date has changed. */
  213. file_access_date_changed (file)
  214.      char *file;
  215. {
  216.   time_t time = (time_t)NULL;
  217.   struct stat finfo;
  218.  
  219.   int index = find_mail_file (file);
  220.   if (index != -1)
  221.     time = mailfiles[index]->access_time;
  222.  
  223.   if (stat (file, &finfo) == 0)
  224.     {
  225.       if (finfo.st_size != 0)
  226.     return (time != finfo.st_atime);
  227.     }
  228.   return (0);
  229. }
  230.  
  231. /* Return non-zero if FILE's size has increased. */
  232. file_has_grown (file)
  233.      char *file;
  234. {
  235.   long size = 0L;
  236.   struct stat finfo;
  237.  
  238.   int i = find_mail_file (file);
  239.   if (i != -1)
  240.     size = mailfiles[i]->file_size;
  241.  
  242.   if (stat (file, &finfo) == 0)
  243.     {
  244.       return (finfo.st_size > size);
  245.     }
  246.   return (0);
  247. }
  248.  
  249. #if defined (USG)
  250. #define DEFAULT_MAIL_PATH "/usr/mail/"
  251. #else
  252. #define DEFAULT_MAIL_PATH "/usr/spool/mail/"
  253. #endif
  254.  
  255. /* Return the colon separated list of pathnames to check for mail. */
  256. char *
  257. get_mailpaths ()
  258. {
  259.   extern char *current_user_name;
  260.   char *mailpaths;
  261.  
  262.   mailpaths = get_string_value ("MAILPATH");
  263.  
  264.   if (!mailpaths)
  265.     mailpaths = get_string_value ("MAIL");
  266.  
  267.   if (!mailpaths)
  268.     {
  269.       mailpaths = (char *)
  270.     alloca (1 + strlen (DEFAULT_MAIL_PATH) + strlen (current_user_name));
  271.  
  272.       sprintf (mailpaths, "%s%s", DEFAULT_MAIL_PATH, current_user_name);
  273.     }
  274.  
  275.   return (savestring (mailpaths));
  276. }
  277.  
  278. /* Remember the dates of the files specified by MAILPATH, or if there is
  279.    no MAILPATH, by the file specified in MAIL.  If neither exists, use a
  280.    default value, which we randomly concoct from using Unix. */
  281. remember_mail_dates ()
  282. {
  283.   char *mailpaths = get_mailpaths ();
  284.   char *mailfile, *extract_colon_unit ();
  285.   int index = 0;
  286.   
  287.   while (mailfile = extract_colon_unit (mailpaths, &index))
  288.     {
  289.       register int i;
  290.       for (i = 0;
  291.        mailfile[i] && mailfile[i] != '?' && mailfile[i] != '%';
  292.        i++);
  293.       mailfile[i] = '\0';
  294.       add_mail_file (mailfile);
  295.     }
  296. }
  297.  
  298. /* check_mail () is useful for more than just checking mail.  Since it has
  299.    the paranoids dream ability of telling you when someone has read your
  300.    mail, it can just as easily be used to tell you when someones .profile
  301.    file has been read, thus letting one know when someone else has logged
  302.    in.  Pretty good, huh? */
  303.  
  304. /* Check for mail in some files.  If the modification date of any
  305.    of the files in MAILPATH has changed since we last did a
  306.    remember_mail_dates () then mention that the user has mail.
  307.    Special hack:  If the shell variable MAIL_WARNING is on and the
  308.    mail file has been accessed since the last time we remembered, then
  309.    the message "The mail in <mailfile> has been read" is printed. */
  310. check_mail ()
  311. {
  312.   register int string_index;
  313.   char *extract_colon_unit ();
  314.   char *current_mail_file, *you_have_mail_message;
  315.   char *mailpaths = get_mailpaths ();
  316.   int index = 0;
  317.   char *dollar_underscore;
  318.  
  319.   dollar_underscore = get_string_value ("_");
  320.  
  321.   if (dollar_underscore)
  322.     dollar_underscore = savestring (dollar_underscore);
  323.  
  324.   while ((current_mail_file = extract_colon_unit (mailpaths, &index)))
  325.     {
  326.       char *t;
  327.       int use_user_notification;
  328.  
  329.       if (!*current_mail_file)
  330.     {
  331.       free (current_mail_file);
  332.       continue;
  333.     }
  334.  
  335.       t = full_pathname (current_mail_file);
  336.       free (current_mail_file);
  337.       current_mail_file = t;
  338.  
  339.       use_user_notification = 0;
  340.       you_have_mail_message = "You have mail in $_";
  341.  
  342.       for (string_index = 0; current_mail_file[string_index]; string_index++)
  343.     if (current_mail_file[string_index] == '?'
  344.         || current_mail_file[string_index] == '%')
  345.       {
  346.         current_mail_file[string_index] = '\0';
  347.         you_have_mail_message = current_mail_file + string_index + 1;
  348.         use_user_notification++;
  349.         break;
  350.       }
  351.  
  352.       if (file_mod_date_changed (current_mail_file))
  353.     {
  354.       WORD_LIST *tlist, *expand_string ();
  355.       char *string_list ();
  356.       int i, file_is_bigger;
  357.       bind_variable ("_", current_mail_file);
  358.  
  359.       /* Have to compute this before the call to add_mail_file, which
  360.          resets all the information. */
  361.       file_is_bigger = file_has_grown (current_mail_file);
  362.  
  363.       add_mail_file (current_mail_file);
  364.  
  365.       i = find_mail_file (current_mail_file);
  366.  
  367.       /* If the user has just run a program which manipulates the
  368.          mail file, then don't bother explaining that the mail
  369.          file has been manipulated.  Since some systems don't change
  370.          the access time to be equal to the modification time when
  371.          the mail in the file is manipulated, check the size also.  If
  372.          the file has not grown, continue. */
  373.       if (i != -1 &&
  374.           (mailfiles[i]->access_time == mailfiles[i]->mod_time) &&
  375.           !file_is_bigger)
  376.         goto next_mail_file;
  377.  
  378.       if (!use_user_notification)
  379.         {
  380.           if (i != -1 &&
  381.           (mailfiles[i]->access_time < mailfiles[i]->mod_time) &&
  382.           file_is_bigger)
  383.         you_have_mail_message = "You have new mail in $_";
  384.         }
  385.  
  386.       if ((tlist = expand_string (you_have_mail_message, 1)))
  387.         {
  388.           char *tem = string_list (tlist);
  389.           you_have_mail_message = (char *)alloca (1 + strlen (tem));
  390.           strcpy (you_have_mail_message, tem);
  391.           free (tem);
  392.         }
  393.       else
  394.         you_have_mail_message = "";
  395.  
  396.       printf ("%s\n", you_have_mail_message);
  397.       dispose_words (tlist);
  398.     }
  399.  
  400.       if (find_variable ("MAIL_WARNING")
  401.       && file_access_date_changed (current_mail_file))
  402.     {
  403.       add_mail_file (current_mail_file);
  404.       printf ("The mail in %s has been read!\n", current_mail_file);
  405.     }
  406.     next_mail_file: 
  407.       free (current_mail_file);
  408.     }
  409.   free (mailpaths);
  410.  
  411.   if (dollar_underscore)
  412.     {
  413.       bind_variable ("_", dollar_underscore);
  414.       free (dollar_underscore);
  415.     }
  416.   else
  417.     unbind_variable ("_");
  418. }
  419.